import { TYPES, countTextLength } from './util'
import Params from './params'
export default class Methods extends Params {
	constructor() {
		super()
	}

	/**
	 * 设置透明度
	 * @param { number } alpha 透明度
	*/
	setAlpha(alpha = 1) {
		const { Context, type } = this
		if (TYPES.includes(type)) {
			Context.globalAlpha = alpha
		} else {
			Context.setGlobalAlpha(alpha)
		}
	}

	/**
	 * 设置填充颜色
	 * @param { String } style 填充颜色
	*/
	setFillStyle(style) {
		const { Context, type } = this
		if (TYPES.includes(type)) {
			Context.fillStyle = style
		} else {
			Context.setFillStyle(style)
		}
	}

	/**
	 * 设置宽度大小
	 * @param { number } width 宽度
	 */
	setLineWidth(width) {
		const { Context, type } = this
		if (TYPES.includes(type)) {
			Context.lineWidth = width
		} else {
			Context.setLineWidth(width)
		}
	}

	/**
	 * 设置描边颜色
	 * @param { String } style 颜色
	*/
	setStrokeStyle(style) {
		const { Context, type } = this
		if (TYPES.includes(type)) {
			Context.strokeStyle = style
		} else {
			Context.setStrokeStyle(style)
		}
	}

	/**
	 * 设置旋转角度
	 * @param { String } x x轴
	 * @param { String } y y轴
	 * @param { String } w 宽度
	 * @param { String } h 高度
	 * @param { Object } rotate 旋转内容
	 * @param { String } rotate.deg 旋转角度
	 * @param { String } rotate.type 类型 topLeft: 中心点在上左 topMiddle 中心点在上中 topRight 中心点在上右
	 *                             middleLeft: 中心点在中左 bottomMiddle 中心点在正中间 middleRight 中心点在中右
	 * 							   bottomLeft: 中心点在下左 bottomMiddle 中心点在下中 middleRight 中心点在下右
	*/
	setRotate(x, y, w, h, rotate) {
		const { Context } = this
		const { deg = 0, type = 'middle' } = rotate
		let rx = x
		let ry = y
		switch (type) {
			case 'topLeft':
				Context.translate(rx, ry)
				Context.rotate(deg * Math.PI / 180)
				Context.translate(-rx, -ry)
				break
			case 'topMiddle':
				rx = x + (w / 2)
				Context.translate(rx, ry)
				Context.rotate(deg * Math.PI / 180)
				Context.translate(-rx, -ry)
				break
			case 'topRight':
				rx = x + w
				Context.translate(rx, ry)
				Context.rotate(deg * Math.PI / 180)
				Context.translate(-rx, -ry)
				break
			case 'bottomLeft':
				ry = y + h
				Context.translate(rx, ry)
				Context.rotate(deg * Math.PI / 180)
				Context.translate(-rx, -ry)
				break
			case 'bottomMiddle':
				rx = x + (w / 2)
				ry = y + h
				Context.translate(rx, ry)
				Context.rotate(deg * Math.PI / 180)
				Context.translate(-rx, -ry)
				break
			case 'bottomRight':
				rx = x + w
				ry = y + h
				Context.translate(rx, ry)
				Context.rotate(deg * Math.PI / 180)
				Context.translate(-rx, -ry)
				break
			case 'middleLeft':
				ry = y + (h / 2)
				Context.translate(rx, ry)
				Context.rotate(deg * Math.PI / 180)
				Context.translate(-rx, -ry)
				break
			case 'middleRight':
				rx = x + w
				ry = y + (h / 2)
				Context.translate(rx, ry)
				Context.rotate(deg * Math.PI / 180)
				Context.translate(-rx, -ry)
				break
			case 'middle':
				rx = x + (w / 2)
				ry = y + (h / 2)
				Context.translate(rx, ry)
				Context.rotate(deg * Math.PI / 180)
				Context.translate(-rx, -ry)
				break
		}
	}

	/**
	 * 设置三角形旋转角度
	 * @param { String } x x轴
	 * @param { String } y y轴
	 * @param { String } w 宽度
	 * @param { String } h 高度
	 * @param { Object } rotate 旋转内容
	 * @param { String } rotate.deg 旋转角度
	 * @param { String } rotate.type 类型 top: 上 left: 左 right: 右 middle: 中心
	 * @param { String } triangType 三角形类型(不支持自定义的三角形 ) right: 直角三角形 isosceles: 等腰三角形
	*/
	setTriangleRotate(x, y, w, h, rotate, triangType) {
		const { Context } = this
		const { deg = 0, type = 'middle' } = rotate
		let rx = x
		let ry = y
		switch (type) {
			case 'top':
				if (triangType == 'right') {
					rx = x
					ry = y
				} else {
					rx = x + w / 2
					ry = y
				}
				Context.translate(rx, ry)
				Context.rotate(deg * Math.PI / 180)
				Context.translate(-rx, -ry)
				break
			case 'left':
				rx = x
				ry = y + h
				Context.translate(rx, ry)
				Context.rotate(deg * Math.PI / 180)
				Context.translate(-rx, -ry)
				break
			case 'right':
				rx = x + w
				ry = y + h
				Context.translate(rx, ry)
				Context.rotate(deg * Math.PI / 180)
				Context.translate(-rx, -ry)
				break
			case 'middle':
				rx = x + w / 2
				ry = y + h / 2
				Context.translate(rx, ry)
				Context.rotate(deg * Math.PI / 180)
				Context.translate(-rx, -ry)
				break
		}
	}

	/**
	 * 计算内容需要显示在画布中间的x轴的位置
	 * @param { Number | String } boxWidth 容器的宽度
	 * @param { Number | String } contentWidth 内容宽度
	 * @param { String } type 类型 center: 水平 right: 右边
	 * @param { Number } offsetRight 距离右边的差值
	 * @returns 
	*/
	computedCenterX(boxWidth, contentWidth, type = 'center', offsetRight = 0) {
		if (type == 'center') {
			return ((boxWidth - contentWidth) / 2) - offsetRight
		}
		if (type == 'right') {
			return boxWidth - contentWidth - offsetRight
		}
		return undefined
	}

	/**
	 * 计算出文字一共有多少列,渲染位置之类
	 * @param { Number } x x轴
	 * @param { Number } y y轴
	 * @param { Number } w 文字宽度
	 * @param { String } text 文字内容
	 * @param { Number } textIndent 首行缩进
	 * @param { Number } lastWidth 最后一行的宽度 
	 * @param { Object } font 字体
	 * @param { Object } line 行高
	 * @param { String } textAlign 文字对齐方式
	 * @param { String } windowAlign 窗口对齐方式
	 * @returns 
	 */
	computedFontTextLineHeight(x, y, w, text, textIndent, lastWidth, font, line, textAlign, windowAlign, color, offsetRight) {
		const { Context, canvasWidth } = this
		const { fontSize } = font
		Context.font = font.style
		// #ifdef MP-TOUTIAO
		// 不知道为啥现在字节跳动一定用这个方法来设置文字大小了. 之前还不用...
		Context.setFontSize(fontSize)
		// #endif
		// console.log('fontSize', fontSize)
		let { lineNum, lineHeight, lineStyle } = line
		// 文字的总长度
		let textLength = countTextLength(Context, text, fontSize)
		if (w == 0 || w == 375.0001) {
			textLength > canvasWidth ? w = canvasWidth : w = textLength + fontSize
		}
		let rowData = []
		function getRowData(text) {
			let txt = ''
			let row = []
			let allData = []
			for (let i in text) {
				txt += text[i]
				let length = countTextLength(Context, txt, fontSize)
				if (textIndent != 0 && row.length === 0) {
					length += textIndent
				}
				if (length >= w - fontSize || i == text.length - 1) {
					let current = row.length == 0 ? 0 : row[row.length - 1].index
					let newAllData = Object.assign([], allData).splice(current, i)
					let limitLastText = lastWidth != 0 && newAllData.length != 0 && newAllData.reverse().find(item => item.length <= lastWidth)
					row.push({
						length,
						text: txt,
						index: i - 1,
						lastText: txt.substring(0, txt.length - 1) + '...',
						limitText: limitLastText ? limitLastText.text + '...' : '',
						limitTextWidth: countTextLength(Context, limitLastText.text + '...', fontSize),
					})
					txt = ''
				}
				allData.push({
					index: i,
					text: txt,
					length: length
				})
			}
			return row
		}
		if (text.includes('\n')) {
			let texts = text.split('\n')
			for (let text of texts) {
				rowData.push(...getRowData(text))
			}
		} else {
			rowData = getRowData(text)
		}
		// 行高
		lineHeight = lineHeight == 1 ? fontSize + 2 : lineHeight

		// 获取一共有多少列
		let lineSize = rowData.length
		// if (lineNum != - 1 && lineNum > lineSize) {
		// 	lineNum = lineSize
		// }
		let size = lineNum != -1 ? lineNum : lineSize
		if (size > lineSize) {
			size = lineSize
		}
		let textArr = []
		for (let i = 0; i < size; i++) {
			let { text, length: textLength, lastText, limitText, limitTextWidth } = rowData[i]
			let obj = {}
			let wx = x
			let tx = x
			if (i == 0 && textIndent != 0 && textAlign == 'none' && windowAlign == 'none') {
				textLength += textIndent
				wx += textIndent
				tx = wx
			}
			if (text && lineNum != -1 && i == lineNum - 1) {
				if (lastWidth > 0) {
					text = limitText
					textLength = limitTextWidth
				} else {
					text = textLength + fontSize >= w ? lastText : text
				}
			}
			if (textAlign != 'none' && textLength <= w) {
				let textW = w
				if (textLength == w) {
					textW = canvasWidth
				}
				tx = this.computedCenterX(textW, textLength, textAlign)
				wx += tx
			}
			if (windowAlign != 'none' && textAlign != 'none') {
				wx = this.computedCenterX(canvasWidth, w, windowAlign, offsetRight)
				wx += tx
				tx = wx
			} else if (windowAlign != 'none') {
				wx = this.computedCenterX(canvasWidth, w, windowAlign, offsetRight)
				tx = wx
			}
			let tw = 0
			let ty = y + (i * lineHeight)
			if (lineStyle != 'none') {
				tx = tx + this.getUnit(0)
				tw = textLength + this.getUnit(0)
				if (lineStyle == 'underline') {
					ty += fontSize + 1
				}
				if (lineStyle == 'lineThrough') {
					ty += fontSize / 2
					// #ifdef MP-WEIXIN
					if (uni.getSystemInfoSync().platform === 'android' && this.type != '2d') {
						ty += font.fontSize / 6
					}
					// #endif
				}
			}
			obj.w = textLength
			obj.text = text
			obj.x = wx
			if (lineHeight == 0) {
				obj.y = y + (fontSize * i)
			} else {
				obj.y = y + (lineHeight * i)
			}
			obj.fontColor = color
			obj.lineColor = color
			obj.tw = tw
			obj.ty = ty
			obj.tx = tx
			obj.ey = obj.y + fontSize
			text && textArr.push(obj)
		}
		return textArr
	}
}